H5x.component("import.addform",[],function(opts){
    this.container =  opts.container;
    this.opts = opts;
    this.use("render");
    this.use("dai",{
        app_name:"home",
        host:global_api_host
    });
    this.use("emitter");

    this.applydai = this.dai.create("apply");
    this.activitydai = this.dai.create("activity");
    this.userdai = this.dai.create("users");
    this.choosendai = this.dai.create("choosen");

    //this.applistdai = this.dai.create("appsetting");
    this.init();

},{
    init:function(){
        var self = this;
        this.activitydai.query({},function(rs){
            //console.log(rs);
            self.userdai.query({},function(rs1){
                //console.log(rs1);
                self.render("tp-import-form",{
                    aclist:rs.list,
                    userlist:rs1.list,
                    apply:{}
                });

                self.bindAction();
            })
        });
    },
    bindAction:function(){
        function resetClazz(){
            var ac = $("select[name='activityid'] option:selected");
            var clazz = ac.attr("data-class");
            var school = ac.html();
            $("input[name='school']").val(school);

            var cls = clazz.split(";");
            cls.forEach(function(item){
                $("select[name='clazz']").append("<option value'"+item+"'>"+item+"</option>");
            });
        }

        $("select[name='activityid']").change(function(){
            resetClazz();
        });

        resetClazz();

        var self = this;
        $("#import-save-btn").click(function(){
            //self.emitter.emit("form-submit");           //console.log($("#import-add-form"));
            var data = $("#import-add-form").getJson();

            /*
            name: String, //姓名
            activityid: String, //活动ID
            status:2,
            tel: String, //电话
            messages: String, //留言
            school: String, //学校
            clazz: String, //班级
            */

            data.messages = "后台添加";
            data.status = 2;

            console.log(data);

            self.applydai.add(data,function(rs){                
                if(rs.code>0){
                    self.applydai.action("photo_complate",{
                        applyid:rs.data.id,
                        photoer:data.photoer,
                        styler:data.styler,
                        guider:data.guider
                    },function(){
                        $.tip("添加成功！");
                        self.opts.modal.modal("hide");                    
                        setTimeout(function(){
                            if(self.opts.onComplate){
                                self.opts.onComplate();
                            }
                        },300);
                    });
                   
                }
            });
        });
    }
});
